<?php

namespace App\Modules\User;


use App\Modules\Free\Models\FreeRecord;
use App\Modules\User\Models\Member;
use App\Modules\Users\Models\Visitor;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Library\Rank\AbstractSingleRank;
use App\Modules\User\Models\User;
use App\Modules\User\Models\UserReferrer;
use DB;

class UnlockRank extends AbstractSingleRank
{
    // protected static $key = "unlock_rank";

    // public static function refresh()
    // {
    //     self::multi(function () {
    //         static::flush();
    //         User::query()
    //             ->where('level', 1)
    //             ->orderBy('id')
    //             ->each(function ($item) {
    //                 $rebate = Member::query()
    //                     ->where('user_id', $item->id)
    //                     ->sum('rebate');
    //                 self::update($item->id, $rebate);
    //             });
    //     });
    // }

    protected static $key = "unlock:rank";

    public static function setKey($prefix)
    {
        self::$key = "unlock:rank:{$prefix}";
    }

    public static function increaseAll($id, $step = 1)
    {
        self::setKey('quarterly');
        self::increase($id, $step);

        self::setKey('annual');
        self::increase($id, $step);

        self::setKey('previous');
        self::increase($id, $step);

        for ($i = 2019; $i < (int)date('Y'); $i++) {
            self::setKey('previous'.$i);
            self::increase($id, $step);
        }
    }

    public static function flush()
    {
        self::setKey('all');
        Redis::del(static::$key);
        
        self::setKey('quarterly');
        Redis::del(static::$key);

        self::setKey('annual');
        Redis::del(static::$key);

        self::setKey('previous');
        Redis::del(static::$key);

        for ($i = 2019; $i < (int)date('Y'); $i++) {
            self::setKey('previous'.$i);
            Redis::del(static::$key);
        }
    }

    public static function refresh()
    {
        static::flush();
        self::multi(function () {
            Member::query()
                ->leftjoin('users', 'users.id', '=', 'members.user_id')
                ->where('users.level', 1)
                ->select('members.*')
                ->orderBy('members.id')
                ->each(function ($item) {
                    // 全部
                    self::setKey('all');
                    self::increase($item->user_id, $item->rebate);

                    if ($item->created_at > now()->startOfQuarter()->toDateTimeString()) {
                        // 季度
                        self::setKey('quarterly');
                        self::increase($item->user_id, $item->rebate);
                    }else if ($item->created_at > now()->startOfYear()->toDateTimeString()){
                        //年度
                        self::setKey('annual');
                        self::increase($item->user_id, $item->rebate);
                    }else if ($item->created_at < now()->startOfYear()->toDateTimeString()) {
                        //往年
                        self::setKey('previous');
                        self::increase($item->user_id, $item->rebate);

                        //年份
                        for ($i = 2019; $i < (int)date('Y'); $i++) { 
                            if (($item->created_at >= $i . '-01-01 00:00:00') && ($item->created_at <= $i . '-12-31 23:59:59')) {
                                self::setKey('previous'.$i);
                                self::increase($item->user_id, $item->rebate);
                            }
                        }

                    }
                });
        });
    }

}
